package com.seaboxdata.interfaceJob.yxzsxt;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.yxzsxt.LinkDataForTgis;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.utils.TransRealHttpsHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
/**
 * @Author Dhjie
 * @Description //TODO 请求道路实时路况接口数据接口
 * @Date  2021/8/25
 * @Param  * @param null
 * @return
 **/

@Component
@Configuration
@Slf4j
public class GetLinkDataForTGisJob {

    @Value("${real.trans.url.GetLinkDataForTGis}")
    private String url;

    @Value("${real.trans.token.GetLinkDataForTGis}")
    private String token;

    @Value("${real.trans.GetLinkDataForTGisTopic}")
    private String topic;

    @Value("${interface.isTest}")
    private String isTest;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    private static final int retry = 3;

    private static int countRetry = 0;

   @Scheduled(cron = "${real.trans.roadOperationMonitoringNow}")//每隔5分钟执行一次
    public void doCollect() {
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
       log.info("调用 道路实时路况接口数据 start");
        try {
            List<LinkDataForTgis> roadOperationMonitorings = retryGetData(url, token);
            if (!CollectionUtils.isEmpty(roadOperationMonitorings)) {
                //写入kafka
                Date date = new Date();
                for (LinkDataForTgis roadOperationMonitoring : roadOperationMonitorings) {
                    roadOperationMonitoring.setCjsj(date);
                    baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(roadOperationMonitoring), topic);
                }
            }
        } catch (Exception e) {
            log.error("调用 道路实时路况接口数据 异常");
            e.printStackTrace();
        }
    }

    public List<LinkDataForTgis> retryGetData(String url, String token) {
        List<LinkDataForTgis> roadOperationMonitorings = null;
        try {
            roadOperationMonitorings = TransRealHttpsHandler.sendPost(url, token, LinkDataForTgis.class);
            if ((LinkDataForTgis) roadOperationMonitorings.get(0) != null && ((LinkDataForTgis) roadOperationMonitorings.get(0)).getSpeed() == null) {
                if (countRetry == retry) {
                    log.error("we have try {} to getData,but still have error", countRetry);
                    return null;
                } else {
                    countRetry++;
                    Thread.sleep(1000);
                    roadOperationMonitorings = retryGetData(url, token);
                }
            } else {
                countRetry = 0;
            }
        } catch (Exception e) {
            log.error("调用 道路实时路况接口数据 异常");
            e.printStackTrace();
        }
        return roadOperationMonitorings;
    }

}
